package org.timeticker.gui;

import org.timeticker.ObjectList;
import org.timeticker.ObjectListListener;

import javax.swing.*;

/**
 * Object list combo box model
 *
 * @author Ivan Latysh
 * @version 0.1
 * @since 11-Aug-2007 : 6:07:37 PM
 */
public class ObjectListComboBoxModel extends AbstractListModel implements ComboBoxModel, ObjectListListener {

  /** Object list */
  protected ObjectList objects;
  /** Selected item */
  protected Object selected;

  /**
   * Return Object list
   *
   * @return objects
   */
  public ObjectList getObjects() {
    return objects;
  }

  public ObjectListComboBoxModel(ObjectList objects) {
    this.objects = objects;
    objects.addListener(this);
  }

  public int getSize() {
    return objects.size();
  }

  public Object getElementAt(int index) {
    return objects.get(index);
  }

  public void setSelectedItem(Object anItem) {
    selected = anItem;
  }

  public Object getSelectedItem() {
    return selected;
  }

  public void objectAdded(ObjectList list, int index, Object object) {
    fireContentsChanged(this, index, index);
  }

  public void objectRemoved(ObjectList list, int index, Object object) {
    fireContentsChanged(this, index, index);
  }

  public void objectChanged(ObjectList list, int index, Object object) {
    fireContentsChanged(this, index, index);
  }

  public void listChanged(ObjectList list) {
    fireContentsChanged(this, 0, getSize()-1);
  }
}
